home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1997 #1
/
Amiga Plus CD - 1997 - No. 01.iso
/
pd
/
sonstiges
/
spontimag10
/
magnet.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-10-15
|
6KB
|
259 lines
/****** Magnet.cpp ***********************************************
*
* PROGRAM
* Magnet
*
* CONTENTS
* Simulates spontaneous magnetization by spin-flipping.
*
* AUTHOR
* Georg Pfundt
*
* COPYRIGHT
* © by Georg Pfundt 14-OCT-1996
*
* LANGUAGE
* ANSI-C++
*
* TRANSLATOR
* SAS-C V6.55
*
* HISTORY
* 1.0, GeP, 14-OCT-1996, first version
*
* SUPPORT
* Dietrich Stauffer; "Theoretische Physik : ein Kurzlehrbuch und
* Repetitorium", Springer Verlag Berlin, Heidelberg, New York, 1989.
* ISBN 3-540-50697-7 (Berlin ...), ISBN 0-387-50697-7 (New York ...)
*
* IMPORTS
* None.
*
* BUGS
* None known.
*
* ADDRESS
* Georg Pfundt, Im Oberviertel 18, 76229 Karlsruhe, Germany
*
* PHONE
* +49 (0)721/481591
* E-Mail: gp@ict.fhg.de
*
* DISCLAIMER
* This program is PUBLIC DOMAIN, as far as it is not used for
* military purposes!
*
********************************************************************
* TABSIZE=2
*/
#include "OWindow.h"
#include <exec/types.h>
#include <proto/Graphics.h>
#include <graphics/rpattr.h>
#include <stdlib.h>
#include <strstream.h>
#include <fstream.h>
/* External libbases for autoinitialisation */
extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;
const char *version = "$VER: Magnet V1.0 by Georg Pfundt " __AMIGADATE__ "\n";
int
main(int argc, char *argv[])
{
const char *usage =
"Magnet (C) by Georg Pfundt "__AMIGADATE__"\n"
"Usage: Magnet [-w width] [-h height] [-l level] [-?] [?]\n"
"width and height are the outer window sizes (default 100 x 110)\n"
"level is the initial magnetization level in permille (default 100=10%)\n"
"-? and ? give this help.\n\n";
// options w and h are width and height
char *opts = "whl",option,*odata;
int next=1;
// Default values for window size
WORD wwidth=100,wheight=110;
LONG level=100;
while ((odata = argopt(argc,argv,opts,&next,&option)) != NULL)
{
switch (option)
{
case 'w':
{
istrstream istr(odata);
istr >> wwidth;
}
break;
case 'h':
{
istrstream istr(odata);
istr >> wheight;
}
case 'l':
{
istrstream istr(odata);
istr >> level;
}
break;
case '?':
cerr << usage;
return 5;
break;
default:
cerr << "Ignored unknown Option \"-" << option << ' ' << odata
<< '\"' << endl;
}
}
// check for help
if (*argv[next] == '?')
{
cerr << usage;
return 5;
}
// Check range of level
level = (level>1000 ? 1000 : level) < 0 ? 0 : level;
// Check range for size
wwidth = (WORD)(wwidth < 0 ? 0x7fff : wwidth);
wheight = (WORD)(wheight < 0 ? 0x7fff : wheight);
wwidth = (WORD)(wwidth < 100 ? 100 : wwidth);
wheight = (WORD)(wheight < 80 ? 80 : wheight);
// Threshold for random numbers
LONG thresh = (((LONG)RAND_MAX & 0xfffffL)*level)/1000;
//cerr << "Parameter: wxh= " << wwidth << " x " << wheight << " level="
// << level << " thresh=" << thresh << endl;
// Construct a Window for output
OWindow mywin("Spontaneous Magnetisation",wwidth,wheight,100,100);
if (!(mywin.IsOK()))
{
cerr << "Error opening Window!" << endl;
return 20;
}
// Get Actuall window dimensions
WORD fwidth=mywin.GetInnerWidth(),fheight=mywin.GetInnerHeight();
// Get offsets
WORD x,y,xof,yof;
xof = mywin.GetLeftOffset();
yof = mywin.GetTopOffset();
// Get actual font
struct TextFont *font;
GetRPAttrs(mywin.RP,RPTAG_Font,&font,TAG_END);
// font size
struct TextExtent texex;
FontExtent(font,&texex);
// reduce height
fheight -= texex.te_Height;
// text position
LONG textx = 5 + texex.te_Extent.MinX + xof;
LONG texty = fheight - texex.te_Extent.MinY + yof;
// Text buffer
char buf[40];
ostrstream ostr(buf,40);
ostr << "Magnet=";
// write-position
int pos = ostr.pcount();
// Set for black and white
SetMaxPen(mywin.RP,1L);
SetWriteMask(mywin.RP,1);
// Set Color and drawing mode
SetABPenDrMd(mywin.RP,1L,0L,JAM1);
// Initalize the screen by random dots
//initial seed for random number
srand((LONG)argv);
LONG magnet=0L;
y=(WORD)(fheight- 1 + yof);
while(--y > yof)
{
x=(WORD)(fwidth- 1 + xof);
while (--x > xof)
if ((rand() & 0xfffffL) <= thresh)
{
WritePixel(mywin.RP,x,y);
magnet--;
}
else
magnet++;
}
const LONG rand_max = RAND_MAX & 0xfffffL;
// Check for new event
while (mywin.CheckMessage() != OWindow::OW_Close)
{
// Output magnetisation
LONG mag2 = (magnet*1000L+500L)/(fwidth-2)/(fheight-2);
ostr.rdbuf()->seekoff(pos,ios::beg);
ostr << (mag2 / 10L) << '.' << ((mag2 < 0 ? -mag2 : mag2) % 10)
<< "% ";
// Get textextent
int chars =
TextFit(mywin.RP,ostr.str(),ostr.pcount()-1,&texex,NULL,1,fwidth-10,fheight);
// Write text
SetDrMd(mywin.RP,JAM2);
Move(mywin.RP,textx,texty);
// If text too wide reduce it
if (chars < (ostr.pcount()-1))
{
chars = TextFit(mywin.RP,ostr.str()+pos,ostr.pcount()-pos-2,&texex,NULL,
1,fwidth-10,fheight);
Text(mywin.RP,ostr.str()+pos,chars);
}
else
Text(mywin.RP,ostr.str(),chars);
// Unlock Streambuf
ostr.rdbuf()->freeze(0);
// Set Drawmode to XOR
SetDrMd(mywin.RP,COMPLEMENT);
// Loop 3000 pixels
WORD i=3000;
while (i--)
{
x = (WORD)(((rand() & 0xfffffL) * (fwidth-2))/rand_max+xof+1);
y = (WORD)(((rand() & 0xfffffL) * (fheight-2))/rand_max+yof+1);
if (ReadPixel(mywin.RP,x-1,y) + ReadPixel(mywin.RP,x+1,y) +
ReadPixel(mywin.RP,x,y-1) + ReadPixel(mywin.RP,x,y+1) == 2)
{
magnet += (ReadPixel(mywin.RP,x,y) << 1) - 1;
WritePixel(mywin.RP,x,y);
}
}
}
return 0;
}